import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:md2_tab_indicator/md2_tab_indicator.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';
import 'package:school_safe_teacher/widget/no_message_widget.dart';


import 'action.dart';
import 'state.dart';

Widget buildView(
    MessageState state, Dispatch dispatch, ViewService viewService) {
  return stateBar(
      child: Column(
        children: [
          Container(
            color: AppColors.toolBarColor,
            padding: EdgeInsets.only(
                left: 15,
                right: 15,
                top: MediaQuery.of(viewService.context).padding.top),
            child: rightTextBar(
                context: viewService.context,
                title: "消息",
                right: "全部已读",
                onTap: () {
                  dispatch(MessageActionCreator.readAll());
                }),
          ),
          Expanded(child: Container(child: _body(state, dispatch, viewService),color: AppColors.MainBackColor,))
        ],
      ),
      context: viewService.context);
}

Widget _body(MessageState state, Dispatch dispatch, ViewService viewService) {
  return EasyRefresh(
      controller: state.easyRefreshController,
      header: MaterialHeader(completeDuration: Duration(milliseconds: 0)),
      footer: MaterialFooter(),
      firstRefresh: true,
      enableControlFinishRefresh: true,
      enableControlFinishLoad: true,
      onRefresh: () async {
        //下拉刷新
        state.page = 1;
        dispatch(MessageActionCreator.onRefresh());
      },
      onLoad: () async {
        //上拉加载
        dispatch(MessageActionCreator.onLoad(++state.page));
      },
      child: _list(state, dispatch, viewService));
}

Widget _list(MessageState state, Dispatch dispatch, ViewService viewService) {
  if (state.items.isEmpty) {
    return noMessageWidget();
  } else {
    return ListView.separated(
      padding: EdgeInsets.only(top: 15,left: 15,right: 15),
      itemBuilder: (BuildContext context, int index) {
        return _item(state, dispatch, viewService, index);
      },
      separatorBuilder: (BuildContext context, int index) {
        return SizedBox(
          height: 10,
        );
      },
      itemCount: state.items.length,
    );
  }
}

Widget _item(
    MessageState state, Dispatch dispatch, ViewService viewService, int index) {
  return Container(
    decoration: BoxDecoration(
      borderRadius: BorderRadius.all(Radius.elliptical(10, 10)),
      color: Colors.white,
    ),
    child: Stack(children: [
      Container(child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                  child: Text(
                    state.items[index].title,
                    style: TextStyle(color: AppColors.deepTextColor, fontSize: 15),
                  )),
              Container(
                child: Text(
                  state.items[index].createTime,
                  style: TextStyle(
                    color: AppColors.smallTextColor,
                    fontSize: 12,
                  ),
                ),
              )
            ],
          ),
          Container(
            margin: EdgeInsets.only(
              top: 15,
            ),
            child: Text(
              state.items[index].content,
              style: TextStyle(color: AppColors.middleTextColor, fontSize: 13),
              maxLines: 1,
            ),
          )
        ],
      ),margin: EdgeInsets.only(left: 15, right: 15),
        padding: EdgeInsets.all(15),
        height: 86,),
      state.items[index].readStatus==1?Container():Align(
        alignment: Alignment.topRight,
        child: Container( // 点
          child: CircleAvatar(
            backgroundColor: Colors.red,
            radius: 6,
          ),
          alignment: Alignment.center,
          width: 6,
          margin: EdgeInsets.only(right: 10, top: 10),
        ),
      )
    ],),
  );
}
